<template>
  <page-header-wrapper>
    <a-card>
      <JeepaySearchForm v-if="$access('ENT_UR_TEAM_SEARCH')" :searchConditionNum="6" :searchFunc="searchFunc" :resetFunc="() => { data.searchData= {} }">
        <jeepay-text-up v-model:value="data.searchData['teamName']" :placeholder="'团队名'" />
        <jeepay-text-up v-model:value="data.searchData['teamNo']" :placeholder="'团队编号'" />
      </JeepaySearchForm>

      <!-- 列表渲染 -->
      <JeepayTable
        ref="infoTable"
        :init-data="true"
        :req-table-data-func="reqTableDataFunc"
        :table-columns="tableColumns"
        :search-data="data.searchData"
        row-key="teamId"
        @btnLoadClose="data.btnLoading=false"
      >
        <template #topBtnSlot>
          <div>
            <a-button 
              v-if="$access('ENT_UR_TEAM_ADD')"
              type="primary"
              class="mg-b-30"
              @click="addFunc"
            >
              <plus-outlined />
              新建
            </a-button>
          </div>
        </template>
        <template #bodyCell="{column,record}">
          <template v-if="column.key === 'statRangeType'">
            <span v-if="record.statRangeType == 'year'">年</span>
            <span v-else-if="record.statRangeType == 'quarter'">季度</span>
            <span v-else-if="record.statRangeType == 'month'">月</span>
            <span v-else-if="record.statRangeType == 'week'">周</span>
          </template>
          <template v-if="column.key === 'op'">
            <JeepayTableColumns>
              <a style="margin-right: 20px;" @click="detailFunc(record.teamId)">详情</a>
              <a v-if="$access('ENT_UR_TEAM_EDIT')" style="margin-right: 20px;" @click="editFunc(record.teamId)">修改</a>
              <a v-if="$access('ENT_UR_TEAM_DELETE')" style="color: red;margin-right: 20px;" @click="delFunc(record.teamId)">删除</a>
            </JeepayTableColumns>
          </template>
        </template>
      </JeepayTable>
    </a-card>

    <!-- 新增 / 修改 页面组件  -->
    <InfoAddOrEdit ref="infoAddOrEdit" :callback-func="searchFunc" />
    <!-- 详情页面组件  -->
    <InfoDetail ref="infoDetail" :callback-func="searchFunc" />
  </page-header-wrapper>
</template>
<script setup lang="ts">
import { API_URL_SYS_USER_TEAM_LIST, req } from '@/api/manage'
import InfoDetail from './Detail.vue'
import InfoAddOrEdit from './AddOrEdit.vue'
import { reactive,ref,getCurrentInstance} from 'vue'
const { $infoBox,$access } = getCurrentInstance()!.appContext.config.globalProperties

const tableColumns = reactive([
  {key:'teamId', title: '团队ID', dataIndex: 'teamId', fixed: 'left' },
  {key:'teamName', title: '团队名', dataIndex: 'teamName' },
  {key:'teamNo', title: '团队编号', dataIndex: 'teamNo' },
  {key:'statRangeType', title: '统计周期', dataIndex: 'statRangeType' },
  {key:'createdAt', title: '创建时间', dataIndex: 'createdAt',},
  {key: 'op',title: '操作',fixed: 'right',align: 'center' } 
])
    const infoTable = ref()
    const infoAddOrEdit =ref()
    const infoDetail = ref()
    const data =reactive ({
      tableColumns: tableColumns,
      searchData: {},
      btnLoading: false
    })


    // 请求table接口数据
 function reqTableDataFunc(params){
      return req.list(API_URL_SYS_USER_TEAM_LIST, params)
    }

   function searchFunc() { // 点击【查询】按钮点击事件
      data.btnLoading = true // 打开查询按钮的loading
      infoTable.value.refTable(true)
    }

  function  addFunc () { // 业务通用【新增】 函数
     infoAddOrEdit.value.show()
    }
  function detailFunc (recordId: any) { // 团队详情页
    infoDetail.value.show(recordId)
  }

  function  editFunc (recordId) { // 业务通用【修改】 函数
     infoAddOrEdit.value.show(recordId)
    }

   function delFunc(recordId) { // 业务通用【删除】 函数
     $infoBox.confirmDanger('确认删除？', '', () => {
        return req.delById(API_URL_SYS_USER_TEAM_LIST, recordId).then(res => {
          $infoBox.message.success('删除成功！')
         infoTable.value.refTable(false)
        })
      })
    }

</script>
